package com.ks.composestudy

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.ks.composestudy.ui.unread

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
//            AndroidAliensRow()
//            AndroidAliensColumn()
//            GameOver()
            AndroidAliensLazyRow()
        }
    }
}

@Composable
fun AndroidAliensLazyRow() {
    val data = listOf(
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
    )

    val state = rememberLazyListState()

    val showScrollToTopButton by remember {
        derivedStateOf {
            state.firstVisibleItemIndex > 0
        }
    }

    LazyColumn(
        modifier = Modifier.fillMaxSize(),
        contentPadding = PaddingValues(
            start = 20.dp,
            end = 20.dp,
            top = 10.dp,
            bottom = 10.dp
        ),
        verticalArrangement = Arrangement.spacedBy(8.dp),
        state = state
    ) {
        items(data) {
            Row(
                verticalAlignment = Alignment.CenterVertically
            ) {
                AndroidAlien(color = Color.Black, modifier = Modifier.size(100.dp))
                Text(
                    color = Color.Black,
                    fontSize = 21.sp,
                    fontStyle = FontStyle.Italic,
                    fontWeight = FontWeight.Bold,
                    text = "Alien:$it"
                )
                Icon(imageVector = Icons.Default.Add, contentDescription = null)
            }
        }
    }

}

@Composable
fun GameOver() {
    Box(
        modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center
    ) {
        Spacer(
            modifier = Modifier.matchParentSize().background(color = Color.Gray.copy(alpha = .7f))
        )
        Text(
            color = Color.White,
            fontSize = 41.sp,
            fontStyle = FontStyle.Italic,
            fontWeight = FontWeight.Bold,
            text = "GAME OVER"
        )
    }
}

@Composable
fun AndroidAliensRow() {
    Row(
        horizontalArrangement = Arrangement.SpaceEvenly,
        verticalAlignment = Alignment.Top,
        modifier = Modifier.fillMaxSize()
    ) {
        AndroidAlien(
            color = Color.Red, modifier = Modifier
                .weight(1f)
                .padding(4.dp)
        )
        AndroidAlien(
            color = Color.Gray, modifier = Modifier
                .weight(2f)
                .padding(4.dp)
        )
        AndroidAlien(
            color = Color.Green, modifier = Modifier
                .weight(1f)
                .padding(4.dp)
        )
    }
}

@Composable
fun AndroidAliensColumn() {
    Column(
        verticalArrangement = Arrangement.SpaceAround,
        horizontalAlignment = Alignment.Start,
        modifier = Modifier.fillMaxSize()
    ) {
        AndroidAlien(
            color = Color.Red, modifier = Modifier
                .size(70.dp)
                .padding(4.dp)
        )
        AndroidAlien(
            color = Color.Green, modifier = Modifier
                .size(70.dp)
                .padding(4.dp)
        )
    }
}

@Composable
fun AndroidAlien(color: Color, modifier: Modifier = Modifier) {
    Image(
        painterResource(R.drawable.ic_launcher_foreground),
        modifier = modifier,
        colorFilter = ColorFilter.tint(color = color),
        contentDescription = "无法读取"
    )
}

@Preview(showBackground = true)
@Composable
fun AndroidAliensPreview() {
//    AndroidAliensRow()
//    AndroidAliensColumn()
//    GameOver()
    AndroidAliensLazyRow()
}